package hero_info

import (
	"go_server/game_server/protocol/pb"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
)

func (s *Hero_info_service) S2C_AddCell(conn ziface.IConnection, roleid int64, cells map[int32]int32) (bool, pb.ErrorCodeManager) {
	if cells != nil && len(cells) > 0 {
		hero := Hero_info_serviceObj.LoadByRoleId(roleid)
		if hero != nil {
			if fcs, ok := hero.AddRoleCell(cells); ok {
				msgS2C := &pb.UpdateCellListS2C{}
				msgS2C.Cells = make([]*pb.HeroCellData, 0)

				for _, cv := range fcs {
					msgS2C.Cells = append(msgS2C.Cells, cv.To_PbData())
				}

				go hero.SaveDB_HeroInfo_Cells()
				defer func() {
					if msgS2C != nil {
						zlog.Debugf("ProtocolManager_UpdateCellList %v", msgS2C)
						defer func() {
							conn.SendBuffPBMsg(uint32(pb.ProtocolManager_UpdateCellList), msgS2C)
						}()
					}
				}()
				return true, pb.ErrorCodeManager_SUCCESS
			}
		}
	}
	return false, pb.ErrorCodeManager_HERO_INFO_NOTEXISTS_CELL
}
